/*
 * <Pezad Platformer>
 * Copyright 2008 Shayne Riley
 * 
 * This file is part of <Pezad Platformer>.
 * 
 * <Pezad Platformer> is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * <Pezad Platformer> is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with <Pezad Platformer>.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * To contact the authors on questions or issues regarding the license,
 * you may send an email to <shayner@gmail.com> or write to
 * Shayne Riley / P.O. Box 1092 / McCook, NE 69001.
 */

#include "ImageSDL.h"
#include "SDLDelegate.h"
#include <iostream> 

namespace pzdDisplay
{

ImageSDL::ImageSDL(SDL_Surface *surface)
: Image(), mSurface(initSurface(surface))
{
	
}

ImageSDL::~ImageSDL()
{
	// The SDL_Surface must always be freed or the memory allocator will
	// scream bloody murder.
	if(mSurface)
	{
		SDL_FreeSurface(mSurface);
	}
}

int ImageSDL::getWidth() const
{
	return mSurface->w;
}

int ImageSDL::getHeight() const
{
	return mSurface->h;
}

SDL_Surface* ImageSDL::initSurface(SDL_Surface *src)
{
	using namespace std;

	// If the source is null, then tell somebody about it, but it looks like
	// we have to keep it like that, unfortunately.
	if(!src)
	{
		// Failure
		cerr << __FILE__ << ": Source SDL_Surface is null. If this ImageSDL is "
				<< "used, bad things may happen." << endl;
		return NULL;
	}

	// If the source is locked, then complain and unlock it.
	if(src->locked)
	{
		// Failure
		cerr << __FILE__ << ": Source SDL_Surface was locked...";
		
		SDL_UnlockSurface(src);
		
		cerr << " but is now unlocked by ImageSDL constructor. This is a big"
				<< "no-no." << endl;
	}
	
	return src;

}

SDL_Surface * ImageSDL::getSurface() const
{
	return mSurface;
}

//SDL_Surface* ImageSDL::initSurface(SDL_Surface *src)
//{
//	using namespace std;
//
//	if(!copiableSurface(src))
//	{
//		// Failure
//		cerr << __FILE__ << " Unable to copy SDL_Surface, cannot create"
//			<< " ImageSDL." << endl;
//		return NULL;
//	}
//	
//	// Create the SDL_Surface. Use the specs from the source image.
//	Uint32 flags = src->flags;
//	int width = src->w;
//	int height = src->h;
//	int depth = src->format->BitsPerPixel;
//	Uint32 rMask = src->format->Rmask;
//	Uint32 gMask = src->format->Gmask;
//	Uint32 bMask = src->format->Bmask;
//	Uint32 aMask = src->format->Amask;
//	
//	SDL_Surface* dest = SDL_CreateRGBSurface(flags, width, height, depth,
//			rMask, gMask, bMask, aMask);
//
//	if(dest == NULL)
//	{
//		cerr << __FILE__ << ": Unable to create surface to copy to. Cannot"
//			<< " create ImageSDL." << endl;
//		return NULL;
//	}
//
//	// Now that the destination is prepared, copy the source to the destination.
//	// Neither surface should be locked.
//	// If the source copy rectangle or the destination copy rectangle are NULL,
//	// then the entire surface is copied, which is what we want.
//	if(int error = SDL_BlitSurface(src, NULL, dest, NULL) != 0)
//	{
//		cerr << __FILE__ << ": Unable to blit. ImageSDL is blank. Error code "
//			<< error << "." << endl;
//	}
//
//	// The operation was a success, unless the blit failed. Then it is blank.
//	return dest;
//}
//
//bool ImageSDL::copiableSurface(SDL_Surface *src)
//{
//	using namespace std;
//
//	// If the source is null, then a copy of it cannot exist either.
//	if(!src)
//	{
//		// Failure
//		cerr << __FILE__ << ": Source SDL_Surface is null." << endl;
//		return false;
//	}
//	
//	// If the source is locked, then the image cannot be constructed.
//	if(src->locked)
//	{
//		// Failure
//		cerr << __FILE__ << ": Source SDL_Surface is locked." << endl;
//		return false;		
//	}
//
//	// SDL_CreateRGBSurface must have SDL_SetVideoMode called first for some
//	// reason.
//	if(!pzd::SDLDelegate::getInstance().needSDLWithWindow())
//	{
//		// Failure
//		cerr << __FILE__ << ": SDL_SetVideoMode failed." << endl;
//		return false;
//	}
//
//	// Success!
//	// If all the previous checks passed, then it is highly probable that the
//	// source surface can be copied from.
//	return true;
//}

} // namespace
